package com.fingard.dsp.bank.directbank.cib02;

import com.fingard.dsp.bank.directbank.bankInterface.ICertificateMailedMsg;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.ReqCertificateMailedMsgQry;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.RetAtsCertificateMailedMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 *  合格证邮寄信息
 *  @author Parker on 2019-11-20.
 * @version 1.0
 */
public class CIB02CertificateMailedMsg extends CIB02Base implements ICertificateMailedMsg {
    @Override
    public RetAtsCertificateMailedMsg getCertificateMailedMsg(ReqCertificateMailedMsgQry qry) throws Exception {
        RetAtsCertificateMailedMsg retAts = new RetAtsCertificateMailedMsg(qry.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";

        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("REQUEST");
            xmlWriter.writeElementString("TRANCODE", "28006");//交易代码
            xmlWriter.writeElementString("COREID", bankFront.org);//直联企业系统客户号
            xmlWriter.writeElementString("YURREF", qry.reqHeader.transSeqID);//业务参考号
            xmlWriter.writeElementString("DLENUM", qry.DealerCode);//经销商代码
            xmlWriter.writeElementString("DLENAM", qry.DealerName);//经销商名称

            xmlWriter.writeElementString("EMLNUM", qry.MailNo);//邮寄单号
            xmlWriter.writeElementString("EMLNAM", qry.MailCompanyName);//快递公司名称
            xmlWriter.writeElementString("EMLCUT", qry.MailNum);//邮寄数量
            xmlWriter.writeElementString("EMLTME", qry.MailTime);//邮寄时间
            xmlWriter.writeElementString("REV50L", qry.BillNo);//保留字段
            while(qry.readRecord()){
                xmlWriter.writeStartElement("DATA");
                xmlWriter.writeElementString("CHANUM", qry.reqDetail.VIN);//底盘号/车架号
                xmlWriter.writeElementString("ORDEID", qry.reqDetail.OrderCode);//订单号
                xmlWriter.writeElementString("CRELON", qry.reqDetail.CertificationNo);//合格证号
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),"T28006");//发送银行

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/RESPONSE/RTNCOD");
                    String tmpRetMsg = tmpBkReader.getFirstText("/RESPONSE/RTNMSG");
                    if ("SCDS000".equals(tmpRetCode)) {
                        while(tmpBkReader.read("/RESPONSE/DATA")){
                            String itemRetCode = tmpBkReader.readedXElement.getSingleTextNotNull("ERRCOD");
                            String itemRetMsg = tmpBkReader.readedXElement.getSingleTextNotNull("ERRMSG");
                            if(itemRetCode.equals("SCDS000")){
                                tmpRespCode = "0";
                                tmpRespInfo = "处理成功";
                            } else{
                                tmpRespCode = "-1";
                                tmpRespInfo = itemRetCode + " : " + itemRetMsg;
                                WriteBankLogLn("失败原因：" + tmpRespInfo);
                                break;
                            }
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetCode + " : " + tmpRetMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("交易出现异常:" + e);
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
